package com.scale.scales.util;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 数字获取及格式化辅助类
 */
public class NumFormatUtil {

    public static final int PASSWORD_TO_OPERATION = 0;
    public static final int PASSWORD_TO_SETTING = 1;
    public static final int PASSWORD_TO_UNIT = 2;

    public static DecimalFormat df3 = new DecimalFormat("######0.000");
    private static NumFormatUtil mUtil = null;
    private static Pattern pattern = Pattern.compile("^(-?[0-9.]+)$");

    private NumFormatUtil() {
    }

    public static NumFormatUtil getInstance() {
        if (mUtil == null) {
            synchronized (NumFormatUtil.class) {
                if (mUtil == null) {
                    mUtil = new NumFormatUtil();
                }
            }
        }
        return mUtil;
    }

//    public static String getBatchNumber(String batchCode) {
//        Date current = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd", Locale.getDefault());
//        String date = sdf.format(current);
//        String companyCode = TextUtils.isEmpty(UserCache.companyCode) ? "00" : addZeroForNum(UserCache.companyCode, 2);
//        return date + companyCode + addZeroForNum(batchCode, 2);
//    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            StringBuilder strBuilder = new StringBuilder(str);
            while (strLen < strLength) {
                strBuilder.insert(0, "0");
                strLen = strBuilder.length();
            }
            str = strBuilder.toString();
        }
        return str;
    }

    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**
     * 是否是数字
     */
    public static boolean isNumeric(String str) {
        return !TextUtils.isEmpty(str) && pattern.matcher(str).matches();
    }

    /**
     * 生成页面显示
     */
    public static String getFormatDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return sdf.format(new Date());
    }

    public static String getHHmm() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return sdf.format(new Date());
    }

    /**
     * 生成交易时间
     */
    public static String getDateDetail() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return format.format(date);
    }

    /**
     * 生成运维中心两道密码
     */
    public static String getRandomPassword(int flag) {
        String pwd = "";
        switch (flag) {
            case PASSWORD_TO_UNIT:
                pwd = "8";
                break;
            case PASSWORD_TO_SETTING:
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1);
                String yesterday = new SimpleDateFormat("MMdd", Locale.getDefault()).format(cal.getTime());
                pwd = yesterday + "37";
                break;
            case PASSWORD_TO_OPERATION:
                pwd = "015";
                break;
        }
        return pwd;
    }

    public BigDecimal getDecimalTare(String t) {
        return this.getDecimalTare(t, 3);
    }

    private BigDecimal getDecimalTare(String t, int s) {
        if (s < 0) {
            s = 3;
        }
        return new BigDecimal(t).setScale(s, BigDecimal.ROUND_HALF_UP);
    }
}